package com.lh.common.utils;

import java.io.UnsupportedEncodingException;
import java.util.Random;

/**
 * 生产指定长度随机字符串a-z,A-Z,0-9
 *
 * @author happyqing
 * @since 2016.5.30
 */
public class RandomString {

	/**
	 * 获取随机字符串 a-z,A-Z,0-9
	 *
	 * @param length
	 *            长度
	 * @return
	 */
	public static String getRandomString(int length) {
		String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < length; ++i) {
			// int number = random.nextInt(62);// [0,62)
			sb.append(str.charAt(random.nextInt(62)));
		}
		return sb.toString();
	}

	/**
	 * JAVA获得0-9,a-z,A-Z范围的随机数
	 *
	 * @param length
	 *            随机数长度
	 * @return String
	 */
	public static String getRandomChar(int length) {
		char[] chr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
				'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
				'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
				'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
				'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
				'X', 'Y', 'Z' };
		Random random = new Random();
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < length; i++) {
			buffer.append(chr[random.nextInt(62)]);
		}
		return buffer.toString();
	}

	/**
	 * 获取随机字符串 a-z
	 *
	 * @param length
	 *            长度
	 * @return
	 */
	public static String getLowerLetter(int length) {
		String str = "abcdefghijklmnopqrstuvwxyz";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; ++i) {
			sb.append(str.charAt(random.nextInt(26)));
		}
		return sb.toString();
	}

	/**
	 * 获取随机字符串 0-9
	 *
	 * @param length
	 *            长度
	 * @return
	 */
	public static String getNumber(int length) {
		String str = "0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; ++i) {
			sb.append(str.charAt(random.nextInt(10)));
		}
		return sb.toString();
	}

	/**
	 * 获取随机字符串 0-9,a-z,0-9 有两遍0-9，增加数字概率
	 *
	 * @param length
	 *            长度
	 * @return
	 */
	public static String getLowerLetterNumber(int length) {
		String str = "0123456789abcdefghijklmnopqrstuvwxyz0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; ++i) {
			sb.append(str.charAt(random.nextInt(46)));
		}
		return sb.toString();
	}

	/**
	 * 获取随机密码，lLength位小写英文+nLength位数字
	 *
	 * @param lLength
	 *            字母长度
	 * @param nLength
	 *            数字长度
	 * @return
	 */
	public static String getPasswordSimple(int lLength, int nLength) {
		return getLowerLetter(lLength) + getNumber(nLength);
	}

	/**
	 * 获取随机密码，包含英文和数字
	 *
	 * @param length
	 *            密码长度
	 * @return
	 */
	public static String getPasswordComplex(int length) {
		String password;
		while (true) {
			password = getLowerLetterNumber(6);
			// 必须包含字母和数字
			if (password.matches("(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}")) {
				return password;
			}
		}
	}

	//自动生成名字（中文）
	public static String getRandomJianHan(int len) {
		String ret = "";
		for (int i = 0; i < len; i++) {
			String str = null;
			int hightPos, lowPos; // 定义高低位
			Random random = new Random();
			hightPos = (176 + Math.abs(random.nextInt(39))); // 获取高位值
			lowPos = (161 + Math.abs(random.nextInt(93))); // 获取低位值
			byte[] b = new byte[2];
			b[0] = (new Integer(hightPos).byteValue());
			b[1] = (new Integer(lowPos).byteValue());
			try {
				str = new String(b, "GBK"); // 转成中文
			} catch (UnsupportedEncodingException ex) {
				ex.printStackTrace();
			}
			ret += str;
		}
		return ret;
	}

	//生成随机用户名，数字和字母组成,
	public String getStringRandom(int length) {

		String val = "";
		Random random = new Random();

		//参数length，表示生成几位随机数
		for(int i = 0; i < length; i++) {

			String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
			//输出字母还是数字
			if( "char".equalsIgnoreCase(charOrNum) ) {
				//输出是大写字母还是小写字母
				int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
				val += (char)(random.nextInt(26) + temp);
			} else if( "num".equalsIgnoreCase(charOrNum) ) {
				val += String.valueOf(random.nextInt(10));
			}
		}
		return val;
	}

	//生成随机用户名，数字和字母组成,
	public static String getRandomPhoneNumber() {
		String phone = "1";

		Random random = new Random();
		int nextInt = random.nextInt(5);
		int num[] = {3,4,5,7,8};
		phone +=  num[nextInt]+getNumber(9);
		return phone;
	}

}
